Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
snakecase-keys
Advanced tools
The snakecase-keys npm package is a utility that converts the keys of an object to snake_case. This can be particularly useful when working with APIs or databases that require snake_case formatting.
Convert Object Keys to Snake Case
This feature allows you to convert the keys of an object from camelCase to snake_case. It works recursively, so nested objects are also converted.
const snakecaseKeys = require('snakecase-keys');
const camelCaseObject = { fooBar: 'baz', nestedObject: { helloWorld: 'value' } };
const snakeCaseObject = snakecaseKeys(camelCaseObject);
console.log(snakeCaseObject); // { foo_bar: 'baz', nested_object: { hello_world: 'value' } }
Exclude Specific Keys
This feature allows you to exclude specific keys from being converted to snake_case. You can pass an array of keys to the `exclude` option.
const snakecaseKeys = require('snakecase-keys');
const camelCaseObject = { fooBar: 'baz', nestedObject: { helloWorld: 'value' }, excludeMe: 'no_change' };
const snakeCaseObject = snakecaseKeys(camelCaseObject, { exclude: ['excludeMe'] });
console.log(snakeCaseObject); // { foo_bar: 'baz', nested_object: { hello_world: 'value' }, excludeMe: 'no_change' }
Deep Conversion
This feature ensures that all nested objects are also converted to snake_case. By default, the conversion is deep, but you can control this behavior with the `deep` option.
const snakecaseKeys = require('snakecase-keys');
const camelCaseObject = { fooBar: 'baz', nestedObject: { helloWorld: 'value' } };
const snakeCaseObject = snakecaseKeys(camelCaseObject, { deep: true });
console.log(snakeCaseObject); // { foo_bar: 'baz', nested_object: { hello_world: 'value' } }
The camelcase-keys package converts object keys to camelCase. It is the inverse of snakecase-keys and can be useful when you need to convert keys from snake_case to camelCase.
Lodash is a utility library that provides a wide range of functions for manipulating objects, arrays, and other data types. It includes a `_.snakeCase` function that can be used to convert strings to snake_case, but it does not provide a direct way to convert object keys like snakecase-keys.
The change-case package provides a set of functions for converting strings between different cases, including snake_case. While it does not directly convert object keys, it can be used in combination with other utilities to achieve similar functionality.
Convert an object's keys to snake case
$ npm install --save snakecase-keys
var snakecaseKeys = require('snakecase-keys')
snakecaseKeys({fooBar: 'baz'})
//=> {foo_bar: 'baz'}
snakecaseKeys({'foo-bar': true, nested: {fooBaz: 'bar'}});
//=> {foo_bar: true, nested: {foo_baz: 'bar'}}
snakecaseKeys(obj, options)
-> object
Required
Type: object
An object to transform into snake case (keys only).
Optional
Type: object
Type: boolean
Default: true
Enables snake-casing of keys in nested objects.
Type: array[string || regexp]
Default: []
An array of strings or regular expressions matching keys that will be excluded from snake-casing.
MIT © Ben Drucker
FAQs
Convert an object's keys to snake case
The npm package snakecase-keys receives a total of 460,206 weekly downloads. As such, snakecase-keys popularity was classified as popular.
We found that snakecase-keys demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.